<?php
?>
<div class="yiiForm">

<?php

$this->widget('application.components.RequiredMessage');

echo CHtml::beginForm('', 'post', array (
	'id' => "topics_selection"
));

echo Yii::t('views', 'Topics available');

$this->printHiddenField($model);

echo CHtml::activeHiddenField($model, 'form_type');
echo CHtml::errorSummary($model);

echo "<div id=\"topics_tree\">\n";
display_items($model, $this, 0, "step", $route->steps, "", 0, false);
echo "</div>";

function display_items($model, $root, $level, $type, $topics, $parent_id, $weeks_before, $odd) {
	if (!is_array($topics)) return $odd;
	if (count($topics) == 0) return $odd;
	$first == true;
	echo "\t<ul class='table'>\n";
	foreach ($topics as $step) {
		echo "\t\t" . '<li><div id="' . $step->key . '" class="' . $type . '_' . ($odd ? 'odd' : 'even') . '">'. "\n";
		$attribute = 'topics['.$step->key.']';
		$opts = array();
		CHtml::resolveNameId($model, $attribute, $opts);
		if ($step->topics != null && count($step->topics) > 0) {
			echo "<input type=\"hidden\" class=\"cb_select_item\" name=\"".$opts['name']."\" id=\"".$opts['id']."\" value=\"-2\" checked>";
		} else {
			echo "<input type=\"checkbox\" class=\"cb_select_item\" name=\"".$opts['name']."\" id=\"".$opts['id']."\" value=\"1\" checked>";
		}
				
		echo "\t\t" . '<li><div id="' . $step->key . '" class="' . $type . '_' . ($odd ? 'odd' : 'even') . '">'. "\n";
		$attribute = 'parent['.$step->key.']';
		$opts = array();
		CHtml::resolveNameId($model, $attribute, $opts);
		if ($step->parent_key != null && $step->parent_key != "")
			echo "<input type=\"hidden\" name=\"".$opts['name']."\" value=\"".$step->parent_key ."\" checked>";
		
		echo "<span id=\"label_" . $step->key . "\" class=\"label\">" . $step->label . "</span>&nbsp;\n\n";
		echo "<span id=\"user_inputs_" . $step->key . "\" class=\"user_inputs\">\n";

		$attribute = 'starts['.$step->key.']';
		$opts = array();
		CHtml::resolveNameId($model, $attribute, $opts);
		$opts['size'] = 12;
		$root->widget('zii.widgets.jui.CJuiDatePicker', array (
					'model' => $model,
					'name' => $opts['name'],
					'value' => date(Constants::DISPLAY_DATE_FORMAT	, mktime(date("H"), date("i"), date("s"), date("n"), ((int)date("j"))+7*($weeks_before+$step->weeks_before), date("Y"))),
					'options' => Constants::$JuiDatePickerOptions,
					'htmlOptions' => $opts
		));
		$attribute = 'ends['.$step->key.']';
		$opts = array();
		CHtml::resolveNameId($model, $attribute, $opts);
		$opts['size'] = 12;
		$root->widget('zii.widgets.jui.CJuiDatePicker', array (
					'model' => $model,
					'name' => $opts['name'],
					'value' => date(Constants::DISPLAY_DATE_FORMAT	, mktime(date("H"), date("i"), date("s"), date("n"), ((int)date("j"))+7*($weeks_before+$step->weeks_before+$step->weeks_number), date("Y"))),
					'options' => Constants::$JuiDatePickerOptions,
					'htmlOptions' => $opts
		));

		echo "</span>\n";
	echo "<div id=\"comment_" . $step->key . "\">";
		/*$root->beginWidget('zii.widgets.jui.CJuiDialog', array(
			     'id'=>'comment_' . $step->key,
		// additional javascript options for the dialog plugin
			     'options'=>array(
			         'title'=>$step->label,
			         'autoOpen'=>false,
			 		 'minWidth' => 800,
					 'minHeight' => 400,
			 		 'width' => 800,
					 'height' => 400,
					'modal' => true,
		),
		));*/


		$attribute = 'adviser_validation['.$step->key.']';
		$opts = array();
		CHtml::resolveNameId($model, $attribute, $opts);
		echo CHtml::checkBox($opts['name'], $model->adviser_validation[$step->key], $opts);
		echo CHtml::label("Validation conseiller", $opts['id']);

		$attribute = 'owner_validation['.$step->key.']';
		$opts = array();
		CHtml::resolveNameId($model, $attribute, $opts);
		echo CHtml::checkBox($opts['name'], $model->owner_validation[$step->key], $opts);
		echo CHtml::label("Validation porteur", $opts['id']);
		
		echo "<div id=\"validation_comment_" . $step->key . "\" class=\"validation\"><b>Commentaires :</b><br />";
		$attribute = 'validation_comment['.$step->key.']';
		$opts = array();
		$opts['rows'] = '5';
		$opts['cols'] = '75';
		CHtml::resolveNameId($model, $attribute, $opts);
		echo CHtml::textArea($opts['name'], $model->validation_comment[$step->key], $opts);
		/*$root->widget('application.extensions.jwysiwyg.JWysiwyg', array (
		   'id' => $opts['id'],
		   'name' => $opts['name'],
			'height'=>'100px',
			'width'=>'600px'
		));*/
		echo "</div>";
		
		$attribute = 'objective['.$step->key.']';
		$opts = array();
		CHtml::resolveNameId($model, $attribute, $opts);
		echo CHtml::checkBox($opts['name'], $model->owner_validation[$step->key], $opts);
		echo CHtml::label("Objectif", $opts['id']);
		
		echo "<div id=\"objective_needs_" . $step->key . "\" class=\"objective\"><b>Besoins :</b><br />";
		$attribute = 'objective_needs['.$step->key.']';
		$opts = array();
		$opts['rows'] = '5';
		$opts['cols'] = '75';
		CHtml::resolveNameId($model, $attribute, $opts);
		echo CHtml::textArea($opts['name'], $model->validation_comment[$step->key], $opts);
		/*$root->widget('application.extensions.jwysiwyg.JWysiwyg', array (
		   'id' => $opts['id'],
		   'name' => $opts['name'],
			'height'=>'100px',
			'width'=>'600px'
		));*/
		
		echo "</div><div id=\"objective_contacts_" . $step->key . "\" class=\"objective\"><b>Contacts :</b><br />";
		$attribute = 'objective_contacts['.$step->key.']';
		$opts = array();
		$opts['rows'] = '5';
		$opts['cols'] = '75';
		CHtml::resolveNameId($model, $attribute, $opts);
		echo CHtml::textArea($opts['name'], $model->validation_comment[$step->key], $opts);
		/*$root->widget('application.extensions.jwysiwyg.JWysiwyg', array (
		   'id' => $opts['id'],
		   'name' => $opts['name'],
			'height'=>'100px',
			'width'=>'600px'
		));*/
	echo "</div></div>";
		

	//$root->endWidget('zii.widgets.jui.CJuiDialog');
	echo "</div>";
	$odd = !$odd;
	$odd = display_items($model, $root, $level+1, "topic", $step->topics, $step->key, $weeks_before, $odd);
	echo "\t\t</li>\n";
	if ($type == "step") {
		$weeks_before = $weeks_before + $step->weeks_number + $step->weeks_before;
		$odd = false;
	}
	$first == false;
	}

	echo "\t</ul>\n\n";
	return $odd;
}

?>

<div class="action">
<?php echo CHtml::submitButton(Yii::t('views', 'Create')); ?>
</div>

<?php echo CHtml::endForm(); ?>

</div>

<?php
// Javascript

$cs = Yii::app()->clientScript;

$all = array();
$roots = array();
$stack = array_merge($route->steps);
while (count($stack) > 0) {
	$item = array_shift($stack);
	//foreach (Step::model()->findAll() as $step) {
	$json = array();
	$json['key'] = $item->key;
	$children = array();
	foreach($item->topics as $tmp) {
		$children[] = $tmp->key;
	}
	$json['children'] = $children;
	$json['parent'] = $item->parent_key;
	$all[$json['key']] = $json;
	$stack = array_merge($stack,$item->topics);
	if ($item instanceof Step) {
		$roots[] = $json['key'];
	}
}


$attribute = '';
$opts = array();
CHtml::resolveNameId($model, $attribute, $opts);

$script = "Item.init(\"".$opts['id'].'_'."\", \"" . Constants::JUI_DATE_FORMAT . "\", " . json_encode($all) .");\n";
$script .= "Item.roots = " . json_encode($roots);

//$script = "";

$cs->registerScript('id', $script, CClientScript::POS_READY);
$cs->registerScriptFile("/js/Exception.js");
$cs->registerScriptFile("/js/DateUtils.js");
$cs->registerScriptFile("/js/StringUtils.js");
$cs->registerScriptFile("/js/Item.js");
$cs->registerCssFile('/css/steps.css');



?>

